package com.android.fragmentdemo
import android.os.Bundle
import android.util.TypedValue
import androidx.appcompat.widget.AppCompatTextView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.android.fragmentdemo.adapters.DynamicItem01Adapter
import com.android.fragmentdemo.adapters.DynamicItem02Adapter
import com.android.fragmentdemo.adapters.DynamicItem03Adapter
import com.android.fragmentdemo.adapters.DynamicItem04Adapter
import com.hengfoxwolf.commonbase.base.BaseActivity2
import com.android.fragmentdemo.databeans.DataFactoryIp
import com.android.fragmentdemo.databinding.ActivityDynamicContainerBinding
import com.android.fragmentdemo.databinding.ExpandLayoutBody2Binding
import com.android.fragmentdemo.defineviews.Dy01ItemDecoration
import com.android.fragmentdemo.defineviews.GridItemDecoration
import com.android.fragmentdemo.defineviews.ListDataItemDecoration

class DynamicContainerActivity :
    BaseActivity2<ActivityDynamicContainerBinding>(ActivityDynamicContainerBinding::inflate) {
    override fun init(savedInstanceState: Bundle?) {
        addTitleView(resources.getString(R.string.txt_title_view), R.id.title_view_01)

        addContentView0()

        addTitleView(resources.getString(R.string.txt_color_square), R.id.title_view_02)

        addContentView1()

        addTitleView(resources.getString(R.string.txt_content_square), R.id.title_view_03)

        addContentView2()

        addTitleView(resources.getString(R.string.txt_number_content_square), R.id.title_view_04)

        addContentView3()
    }

    private fun addTitleView(content: String, textViewId: Int) {
        val titleView = AppCompatTextView(this)
        titleView.apply {
            id = textViewId
            text = content
            textSize = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP,
                8F,
                resources.displayMetrics
            )
            setTextColor(resources.getColor(R.color.color_00BFA5))
        }
        val layoutParams = LinearLayoutCompat.LayoutParams(
            LinearLayoutCompat.LayoutParams.WRAP_CONTENT,
            LinearLayoutCompat.LayoutParams.WRAP_CONTENT
        )
        layoutParams.topMargin = resources.getDimension(R.dimen.dp_10).toInt()
        layoutParams.leftMargin = resources.getDimension(R.dimen.dp_10).toInt()
        mBinding.dyClRoot.addView(titleView, layoutParams)
    }

    private fun addContentView0() {
        val mBodyBinding =
            DataBindingUtil.inflate<ExpandLayoutBody2Binding>(
                layoutInflater,
                R.layout.expand_layout_body2,
                null,
                false
            )
        val dyAdapter = DynamicItem01Adapter()
        dyAdapter.itemDataSet = DataFactoryIp.createCharStr(10)
        mBodyBinding.expandViewRv.apply {
            layoutManager = LinearLayoutManager(
                this@DynamicContainerActivity,
                LinearLayoutManager.HORIZONTAL,
                false
            )
            adapter = dyAdapter
            addItemDecoration(
                Dy01ItemDecoration(
                    this@DynamicContainerActivity.resources.getDimension(
                        R.dimen.dp_10
                    )
                )
            )
        }

        val layoutParams = LinearLayoutCompat.LayoutParams(
            LinearLayoutCompat.LayoutParams.MATCH_PARENT,
            LinearLayoutCompat.LayoutParams.WRAP_CONTENT
        )
        layoutParams.leftMargin = resources.getDimension(R.dimen.dp_10).toInt()
        layoutParams.topMargin = resources.getDimension(R.dimen.dp_10).toInt()
        mBinding.dyClRoot.addView(mBodyBinding.root, layoutParams)
    }


    private fun addContentView1() {
        val mBodyBinding =
            DataBindingUtil.inflate<ExpandLayoutBody2Binding>(
                layoutInflater,
                R.layout.expand_layout_body2,
                null,
                false
            )
        val dyAdapter = DynamicItem02Adapter()
        dyAdapter.itemDataSet = DataFactoryIp.createColorList()
        mBodyBinding.expandViewRv.apply {
            layoutManager = GridLayoutManager(
                this@DynamicContainerActivity,
                4
            )
            adapter = dyAdapter
            addItemDecoration(GridItemDecoration(this@DynamicContainerActivity, 4))
        }

        val layoutParams = LinearLayoutCompat.LayoutParams(
            LinearLayoutCompat.LayoutParams.MATCH_PARENT,
            LinearLayoutCompat.LayoutParams.WRAP_CONTENT
        )
        //layoutParams.leftMargin = resources.getDimension(R.dimen.dp_10).toInt()
        //layoutParams.topMargin = resources.getDimension(R.dimen.dp_10).toInt()
        mBinding.dyClRoot.addView(mBodyBinding.root, layoutParams)
    }

    private fun addContentView2() {
        val mBodyBinding =
            DataBindingUtil.inflate<ExpandLayoutBody2Binding>(
                layoutInflater,
                R.layout.expand_layout_body2,
                null,
                false
            )
        val dyAdapter = DynamicItem03Adapter()
        dyAdapter.itemDataSet = DataFactoryIp.createStrDataSet().subList(0,12)
        mBodyBinding.expandViewRv.apply {
            layoutManager = LinearLayoutManager(
                this@DynamicContainerActivity,
            )
            adapter = dyAdapter
            addItemDecoration(ListDataItemDecoration(this@DynamicContainerActivity).apply {
                setDrawLastLine(false)
            })
        }

        val layoutParams = LinearLayoutCompat.LayoutParams(
            LinearLayoutCompat.LayoutParams.MATCH_PARENT,
            LinearLayoutCompat.LayoutParams.WRAP_CONTENT
        )
        layoutParams.leftMargin = resources.getDimension(R.dimen.dp_10).toInt()
        layoutParams.rightMargin = resources.getDimension(R.dimen.dp_10).toInt()
        layoutParams.topMargin = resources.getDimension(R.dimen.dp_10).toInt()
        mBinding.dyClRoot.addView(mBodyBinding.root, layoutParams)
    }

    private fun addContentView3() {
        val mBodyBinding =
            DataBindingUtil.inflate<ExpandLayoutBody2Binding>(
                layoutInflater,
                R.layout.expand_layout_body2,
                null,
                false
            )
        val dyAdapter = DynamicItem04Adapter()
        dyAdapter.itemDataSet = DataFactoryIp.createDataSet()
        mBodyBinding.expandViewRv.apply {
            layoutManager = GridLayoutManager(
                this@DynamicContainerActivity,
                3
            )
            adapter = dyAdapter
            addItemDecoration(GridItemDecoration(this@DynamicContainerActivity, 3).apply {
                setAddExtraSpace(true)
            })
        }

        val layoutParams = LinearLayoutCompat.LayoutParams(
            LinearLayoutCompat.LayoutParams.MATCH_PARENT,
            LinearLayoutCompat.LayoutParams.WRAP_CONTENT
        )
        mBinding.dyClRoot.addView(mBodyBinding.root, layoutParams)
    }

    companion object {
        const val TAG = "DynamicContainerActivity"
    }
}